<?php

/**
 * This is the model class for table "{{comments}}".
 *
 * The followings are the available columns in table '{{comments}}':
 * @property integer $comment_id
 * @property string $comment_desc
 * @property integer $news_id
 * @property integer $comment_time
 * @property string $user_name
 * @property integer $user_id
 *
 * The followings are the available model relations:
 */
class Comments extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Comments the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{comments}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('comment_desc, news_id, comment_time, user_name, user_id', 'required'),
			array('news_id, comment_time, user_id', 'numerical', 'integerOnly'=>true),
			array('user_name', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('comment_id, comment_desc, news_id, comment_time, user_name, user_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'news' => array(self::BELONGS_TO, 'News', 'news_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'comment_id' => 'Comment',
			'comment_desc' => '我说两句',
			'news_id' => 'News',
			'comment_time' => 'Comment Time',
			'user_name' => 'User Name',
			'user_id' => 'User',
		);
	}

	public function scopes(){
		return array(
			'recently'=>array(
				'order'=>'comment_time DESC',
				'limit'=>5,
			),
			'my'=>array(
				'condition'=>'user_id = '.Yii::app()->user->id,
			),
		);
	}
	public function recently($limit=5){
		$this->getDbCriteria()->mergeWith(array(
			'order'=>'comment_time DESC',
			'limit'=>$limit,
		));
		return $this;
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('comment_id',$this->comment_id);
		$criteria->compare('comment_desc',$this->comment_desc,true);
		$criteria->compare('news_id',$this->news_id);
		$criteria->compare('comment_time',$this->comment_time);
		$criteria->compare('user_name',$this->user_name,true);
		$criteria->compare('user_id',$this->user_id);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	public function getUrl()
	{
		
		return '#c'.$this->comment_id;
	}
}